import { CDTimer } from "../etc/CDTimer";
import { BoutModuleC } from "../modules/BoutModule";
import { PlayerModuleC } from "../modules/PlayerModule";
import { MotionModuleC } from "../motion/MotionModule";
import MainUI_Generate from "../ui-generate/MainUI_generate"

export class MainUI extends MainUI_Generate{
    private boutMD:BoutModuleC
    private attackCD:CDTimer = new CDTimer()

    onStart(){
        this.layer = UILayerBottom

        this.boutMD = ModuleService.getModule(BoutModuleC)

        this.boutMD.onPlayerSignStateChanged.add(this.onSignupStateChanged, this)

        this.btn_atk.onPressed.add(this.onSkillClicked.bind(this));
        this.btn_jump.onPressed.add(this.onJumpClicked.bind(this));
        this.btn_signup.onClicked.add(()=>{
            if(this.boutMD.signedFighting) this.boutMD.send_exit_fighting()
            else this.boutMD.send_signup_fighting()
        })

        this.attackCD.resetCD(2200)
        
        this.canUpdate = true
    }

    onShow(){
        this.onSignupStateChanged()
    }

    onHide(){
        
    }

    onUpdate(dt:number){
        if(this.attackCD.getLeftTime() <= 0) {
            this.mask_atk.fanShapedValue = 1
            return
        }
        this.mask_atk.fanShapedValue = 1 - this.attackCD.getLeftTime() / this.attackCD.getCD()
    }

    private onSignupStateChanged(){
        let signed = this.boutMD.signedFighting
        this.img_signup_mask.visibility = signed ? SlateVisibility.Hidden : SlateVisibility.Visible

        this.txt_signup.text = signed ? '取消准备' : '准备战斗'
    }

    private onJumpClicked(){
        let playerMD = ModuleService.getModule(PlayerModuleC)
        playerMD.jump()
    }

    private onSkillClicked(){
        if(!this.attackCD.canUse()) return
        let playerMD = ModuleService.getModule(PlayerModuleC)
        playerMD.attack()
        this.attackCD.mark()
    }
}